package cn.sxt.clinic.controller;


import cn.sxt.clinic.pojo.*;
import cn.sxt.clinic.service.IRegisteredService;
import cn.sxt.clinic.service.IRegistrationDetailService;
import cn.sxt.clinic.service.IRegistrationService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2020-06-29
 */
@Controller
@RequestMapping("/registration")
public class RegistrationController {

    @Autowired
    private IRegistrationService registrationService;
    @Autowired
    private IRegistrationDetailService registrationDetailService;



    @RequiresPermissions("registration:registrationManager")
    @RequestMapping("/registrationManager")
    public String registrationManager(){
        return "registrationManager";
    }


    @RequiresPermissions("registration:list")
    @RequestMapping("/list")
    @ResponseBody
    public ResultObject list(@RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "10")Integer pageSize,
                             String keyword){

        //分页对象
        Page<Registration> page = new Page<>(pageNum, pageSize);
        //条件对象
        QueryWrapper<Registration> queryWrapper = new QueryWrapper<>();

        if (StringUtils.isNotBlank(keyword)){
            //根据挂号类型模糊查询
            queryWrapper.like("registration_type",keyword);
        }

        //查询结果
        Page<Registration> result = registrationService.page(page,queryWrapper);
        return new ResultObject(result);
    }

    @RequiresPermissions("registration:delete")
    @RequestMapping("/delete")
    @ResponseBody
    public MessageObject delete(long registrationId){


        UpdateWrapper<RegistrationDetail> wrapper = new UpdateWrapper<>();
        wrapper.eq("registration_id",registrationId);
        registrationDetailService.remove(wrapper);

        boolean flag = registrationService.removeById(registrationId);

        MessageObject mo = new MessageObject(1, "删除数据成功");
        if (!flag){
            mo = new MessageObject(0, "删除数据失败,请联系管理员");
        }
        return mo;
    }

    @RequestMapping("/registrationEdit")
    public String registeredEdit(Model m, Long registrationId){
        //如果id为空，添加，为空，修改
        if (registrationId != null){
            Registration registration = registrationService.getById(registrationId);

            QueryWrapper<RegistrationDetail> wrapper = new QueryWrapper<>();
            wrapper.eq("registration_id",registrationId);
            List<RegistrationDetail> list = registrationDetailService.list(wrapper);

            registration.setRegistrationDetails(list);
            m.addAttribute("registration",registration);
        }

        return "registrationEdit";
    }

    @RequestMapping("/detail")
    @ResponseBody
    public List<RegistrationDetail> detail(Long registrationId){

        QueryWrapper<RegistrationDetail> wrapper = new QueryWrapper<>();
        //订单id查询条件
        wrapper.eq("registration_id",registrationId);

        List<RegistrationDetail> list = registrationDetailService.list(wrapper);

        return list;
    }


    @RequiresPermissions("registration:insert")
    @RequestMapping("/insert")
    @ResponseBody
    public MessageObject insert(@RequestBody Registration registration) {

        boolean flag = registrationService.insertOrUpdate(registration);

        MessageObject mo = new MessageObject(1, "新增数据成功");
        if (!flag) {
            mo = new MessageObject(0, "新增数据失败,请联系管理员");
        }
        return mo;
    }

    @RequiresPermissions("registration:update")
    @RequestMapping("/update")
    @ResponseBody
    public MessageObject update(@RequestBody Registration registration) {

        boolean flag = registrationService.insertOrUpdate(registration);

        MessageObject mo = new MessageObject(1, "修改数据成功");
        if (!flag) {
            mo = new MessageObject(0, "修改数据失败,请联系管理员");
        }
        return mo;
    }

}

